<template>
  <div class="wrap">
    <h3>换绑手机</h3>
    <div class="top">
      <img src="../../../static/zwf/phone1.png" v-if="img1"/>
      <img src="../../../static/zwf/phone11.png" v-else/>
      <img src="../../../static/zwf/phone2.png" v-if="img2"/>
      <img src="../../../static/zwf/phone22.png" v-else/>
      <img src="../../../static/zwf/phone3.png" v-if="img3"/>
      <img src="../../../static/zwf/phone33.png" v-else />
    </div>
    <div class="bot" v-show="img1">
      <div class="iphoneCode">
        <input class="inp" v-model="iphone"  type="text" disabled>
      </div>
      <p class="false">
        <span v-if="bol"><img src="../../../static/login/alert.png" alt="">手机号码不正确,请重新输入</span>
      </p>
      <div class="clearfix yzm">
        <input class="inp-yzm" v-model="code" type="text" placeholder="验证码">
        <Code class="code" ref="ziy"></Code>
      </div>
      <input class="inp" v-model="password" type="password" placeholder="密码">
      <button class="btn1" @click="yanzheng()">提交验证</button>
    </div>



    <div class="bot" v-show="img2">
      <div class="iphoneCode">
        <input class="inp" @blur="yzIphone()" v-model="iphone1"  type="text" placeholder="请输入手机号">
        <img v-if="iphoneTrue" src="../../../static/login/check .png" height="12" width="12"/>
      </div>
      <p class="false">
        <span v-if="bol"><img src="../../../static/login/alert.png" alt="">手机号码不正确,请重新输入</span>
      </p>
      <div class="clearfix yzm">
        <input class="inp-yzm" v-model="code" type="text" placeholder="验证码">
        <Code class="code" ref="zi"></Code>
      </div>
      <div class="iphoneyzm">
        <input class="inp " :disabled="dis1" v-model="iphoneyzm"  type="text" placeholder="手机验证码">
        <button @click="getCode()" :class="yzm?'yzmbtn':'aa'" :disabled='dis2'>
          {{tit}}
        </button>
      </div>
      <button class="btn1" @click="yanzheng2()">提交验证</button>
    </div>

    <div class="bot" v-show="img3">
        <div class="tishi">
          <img src="../../../static/zwf/mine/dui.png" />
          恭喜您成功修改
        </div>
    </div>





  </div>
</template>

<script>
  import Code from '../com/Code'
  import axios from 'axios'
    export default {
        name: "Changephone",
      data(){
          return{
            img1:true,
            img2:false,
            img3:false,

            iphone:this.$store.state.phone,
            iphone1:'',
            password:'',
            iphoneyzm:'',
            code:'',
            iphoneTrue:false,
            bol:false,
            dis1:true,
            dis2:false,
            yzm:true,
            tit:'获取验证码',
            num:60,
          }
      },
      components:{
          Code
      },
      methods:{
        getCode(){
          this.yzm=false;
          this.dis2=true;
          this.dis1=false;
          this.iphonecode=1111;
          this.tit=this.num+'s';
          var timmer= setInterval(function(){
            this.num--;
            this.tit=this.num+'s';
            if(this.num==0){
              clearInterval(timmer);
              this.yzm=true;
              this.dis2=false;
              this.tit='获取验证码';
              this.num=3;
            }
          }.bind(this),1000)
        },
        yanzheng(){
          if(this.code != this.$refs.ziy.str2){
            alert('验证码不正确,请重新输入')
            this.$refs.ziy.changeImg();
            this.code='';
            return false;
          }
          if(this.password.trim()==''){
            alert('密码不能为空');
            return false;
          }else{
            var params=new URLSearchParams();
            params.append('iphone',this.iphone);
            params.append('pwd',this.password);

            axios.post('/api/firday/login.php',params).then(res=>{
              console.log(res.data);
              if(res.data.code ==1){
                this.img1=false;
                this.img2=true;
                this.img3=false;
                this. password='';
                this.code='';
              }else{
                alert('密码不正确,请重新输入');
                this. password='';
              }
            });
          }

        },
        yzIphone(){
          var reg=/^1[34578]\d{9}$/;
          if(!reg.test(this.iphone1)){
            this.bol=true;
            this.iphoneTrue=false;
            // this.iphone='';
            return false;
          }else{
            this.bol=false;
            this.iphoneTrue=true;
          }
        },
        yanzheng2(){
          if(this.iphone1.trim()==''){
            alert('手机号不能为空');
            return false;
          }
          if(this.code != this.$refs.zi.str2){
            alert('验证码不正确,请重新输入')
            this.$refs.zi.changeImg();
            this.code='';
            return false;
          }else if(this.iphoneyzm!=this.iphonecode){
            if(this.iphoneyzm.trim()==''){
              alert('验证码不能为空');
              return false;
            }else{
              alert('手机验证码不正确,请重新输入');
              return false;
            }
          }else{
            var params=new URLSearchParams();
            params.append('iphone',this.iphone);
            params.append('pwd',this.password);
            params.append('iphone1',this.iphone1);

            axios.post('/api/firday/rephone.php',params).then(res=>{
              if(res.data.code==1){
                this.img1=false;
                this.img2=false;
                this.img3=true;
                this.$store.commit('showIphone',this.iphone);
                this.$store.commit('showPhone',this.iphone);
              }
            })
          }
        },


      }
    }
</script>

<style scoped>
  .wrap{
    width: 1083px;
    height: 623px;
    border: 1px solid #e7e7e7;
  }
  .wrap h3{
    width: 1063px;
    height: 56px;
    line-height: 56px;
    padding-left: 20px;
    font-size: 18px;
    color: #212121;
    border-bottom: 1px solid #e7e7e7;
  }
  .top{
    margin:40px;
  }
  .top img{
    margin-right: -10px;
  }

  .bot{
    width: 1043px;
    padding-left:40px ;
  }
  .iphoneCode{
    position: relative;
    width: 305px;
    height: 40px;
  }
  .iphoneCode img{
    position: absolute;
    top: 16px;
    right: 16px;
  }
  .inp{
    width: 281px;
    height: 16px;
    border: 1px solid #d3d3d3;
    padding:12px;
    font-size: 16px;
    background: #f9f9f9;
  }
  .false{
    width: 305px;
    height: 30px;
    line-height: 30px;
  }
  .false span{
    color: #db2630;
  }
  .false span>img{
    width: 13px;
    height: 13px;
    margin-right: 5px;
    vertical-align: middle;
  }
  .yzm{
    display: flex;
    justify-content: space-between;
    width: 305px;
    height: 44px;
    margin-bottom: 30px;
  }

  .inp-yzm{
    width: 60px;
    height: 16px;
    padding:12px;
    border: 1px solid #d3d3d3;
    font-size: 16px;
    background: #f9f9f9;
    float: left;
  }
  .code{
    float: right;
    height: 40px;
  }

  .btn1{
    width: 150px;
    height: 50px;
    color: #fff;
    background: #f08200;
    border-radius: 5px;
    font-size: 20px;
    display: block;
    margin-top: 40px;
    cursor: pointer;
  }


  .iphoneyzm{
    position: relative;
    width: 305px;
    height: 18px;
    margin-top: 30px;
    margin-bottom: 30px;
    background: red;
  }
  .iphoneyzm .yzmbtn{
    position: absolute;
    top: 5px;
    right: 10px;
    width: 100px;
    height: 30px;
    font-size: 14px;
    border-radius: 0;
    background: #498e3d;
    color: #fff;
  }
  .iphoneyzm .aa{
    position: absolute;
    top: 5px;
    right: 10px;
    width: 100px;
    height: 30px;
    font-size: 14px;
    border-radius: 0;
    background: #f2f2f2;
    color: #999;
  }
  .tishi{
    color: #ec6a17;
    font-size: 24px;
    margin-top: 200px;
    margin-left: 200px;
  }
  .tishi img{
    vertical-align: middle;
  }
</style>
